﻿<?php
/********************************************************
 *
 * 此文件的作用:
 *		包括用户的所有操作
 *		更改权限等
 *
 * @copyright Copyright (c) 2008-2009
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or header("HTTP/1.0 404 Not Found");

FLEA::loadClass('Common_Model');

class Model_User extends Common_Model {

	function __construct() {
		parent::__construct();
		$this->_tb = FLEA::getSingleton('Table_User');
	}

	function getList() {
		$result = $this->_tb->findAll();
		return $result;
	}

	function login($data, $imgcode = false) {
		$rbac = & FLEA::getSingleton('FLEA_RBAC');
		if ( null != $rbac->getUser() )
			return '您已经登录';
		if ( true == $imgcode ) {
			$checkImgcode = $this->checkImgcode($data['imgcode']);
			if ( null != $checkImgcode )
				return $checkImgcode;
		}
		$pwd = substr($data['password'], 3) . substr($data['password'], 0, 3) . 'qgzxs';
		//$user = $this->_tb->validateUser($data['username'], $pwd, true);
		$user = $this->_tb->findByUsername($data['username']);
		if (!$user || !$this->_tb->validateUser($data['username'], $pwd, true)) {
			return '用户名或密码错误';
		}

//		if( false == $user ) {
//			return '用户名或密码错误';
//		}
			
		$roles = $this->_tb->fetchRoles($user);
		
		$sessionuser = array(
			'userid' => $user['user_id'],
			'num' =>  $user['num'],
			'username' => $user['username'],
		);
		$rbac->setUser($sessionUser, $roles);
		//$rbac->setUser($sessionUser);
		return null;
	}


	function register($data, $hadImgcode = false){
		$result = array();
		if ( !is_array($data) ) {
			$result[] = '提交的数据为空！';
			return $result;
		}

		if ( true == $hadImgcode )
			if ( $re = $this->checkImgcode($data['imgcode']) != null )
				$result[] = $re;
		
		if( $this->_tb->existsUsername($data['username']) )
			$result[] = '该用户名已经存在!';
		if ( $this->_tb->existsEmail($data['email']) )
			$result[] = '该邮箱已经注册过!';
        if ( $data['confirmPassword'] != $data['password'] )
            $result[] = '两次输入的密码不一致!';
		if ( count($result) > 0 )
			return $result;
		$data['answer'] = md5($data['answer']);
		$data['password'] = substr($data['password'], 3) . substr($data['password'], 0, 3) . 'qgzxs';
		$re = $this->getUpImages();
		if ($re['image'] == null) $re['image']='www.jpg';
		$data['pic'] = $re['image'];
		$uid = $this->_tb->create($data);
		$data['password'] = $data['confirmPassword'];
		$this->login($data);
		return $result;
	}

	function update($data) {
		$result = array();
		if ( !is_array($data) ) {
			$result[] = '提交的数据为空！';
			return $result;
		}
		$re = $this->getUpImages();
		if (!empty($re['err'])) return $re['err'];
		$data['pic'] = $re['image'];
		if ( !empty($data['pic']) ) {
			$old = $this->_tb->find($data['user_id'], null, 'pic');
			!empty($old['pic']) && @unlink(dirname(APP_DIR) . '/upFiles/pic/' . $old['pic']);
		} else {
			unset($data['pic']);
		}
		$this->_tb->update($data);
		return $result;
	}

	function getUserInfo($cond, $type = null) {
		if ( 'work' == $type )
			$this->_tb->enableLink('work');
		if ( 'teacher' == $type )
			$this->_tb->enableLink('teacher');

		$data = $this->_tb->find($cond);
		if (empty($data)) return null;
		//$data['userType'] = 1 == $data['userType'] ? '硕士' : 0 == $data['userType'] ? '本科' : '博士';
		if ($data['userType'] == 0) {
			$data['userType'] ='本科内招';
		}elseif ($data['userType'] == 1) {
			$data['userType'] = '自筹研究生';
		}elseif ($userType == 2) {
			$data['userType'] == '教师';
		}
		!empty($data['pic']) && $data['pic'] = Up_DIR . '/pic/' . $data['pic'];
		//$data['poor'] = 1 == $data['poor'] ? '是' : '否';
		if ($data['poor'] == 0) {
			$data['poor'] = '无贫困证明';
		}elseif ($data['poor'] == 1) {
			$data['poor'] = '有绿卡';
		}elseif ($data['poor'] == 2) {
			$data['poor'] = '无绿卡但有贫困证明';
		}
		
		if ($data['politics'] == 0) {
			$data['politics'] = '共产党员';
		}elseif ($data['politics'] == 1) {
			$data['politics'] = '共青团员';
		}elseif ($data['politics'] == 2) {
			$data['politics'] = '其它';
		}
		$data['sex'] = 1 == $data['sex'] ? '女' : '男';

		if ( 'work' == $type ) {
			$data['work']['pic'] = Up_DIR . $data['work']['pic'];
			//$data['work']['family'] = 0 == $data['work']['family'] ? '困难' : 1 == $data['work']['family'] ? '一般' : '较好';
			if($data['work']['family'] == 0) {
				$data['work']['family'] = '困难';
			}elseif ($data['work']['family'] == 1) {
				$data['work']['family']= '一般';
			}elseif ($data['work']['family'] == 2) {
				$data['work']['family'] = '较好';
			}
			$courses = split('\|', $data['work']['course']);
			foreach ( $courses as $v ) {
				$v = split('_', $v);
				$course[$v[0].$v[1]] = '*';
			}
			$office = split('\|', $data['work']['office']);
			$office_count=count($office); //这个用于报名那里，限报办公室岗位个数
			if($office[0]=='0') $office_count=0;	//临时出错，所以做成这样~
			$works = array(
				1 => '课室服务组',
				2 => '饭堂服务组',
				3 => '多媒体教室管理组',
				//4 => '粤港澳青年交流中心',
				9 => '金陵四广场管理组',
				//21 => '机关部处',
				5 => '学生活动中心管理组',
				6 => '建阳服务组',
				7 => '图书馆',
				71 => '图书馆流通部',
				72 => '图书馆办公室',
				73 => '图书馆阅览部',
				74 => '图书馆华人中心',
				75 => '图书馆采编部',
				76 => '图书馆技术部',
				77 => '图书馆总服务台',
				78 => '图书馆古籍室',
				79 => '图书馆期刊现报',
				20 => '办公室执行副助理',
				21 => '办公室物业管理部',
				22 => '办公室综合服务部',
				23 => '办公室市场部',
				24 => '办公室财务部',
				25 => '办公室人力部',
				26 => '办公室推广部'
			);
			$data['work']['group'] = $works[$data['work']['group']];
			$data['work']['course'] = $course;
			$data['work']['office0'] = $works[$office[0]];
			$data['work']['office1'] = $works[$office[1]];
			if($office[0]=='' && $office[1]=='') $office_count=0;
			$data['work']['office_count'] = $office_count;
			//$data['work']['office'] = $works[$data['work']['office']];
			$data['work']['change'] = 1 == $data['work']['change'] ? '是' : '否';
		}

		if ( 'teacher' == $type ) {
			$data['teacher']['experience'] = 1 == $data['teacher']['experience'] ? '是' : '否';
			$data['teacher']['mandarin'] = 1 == $data['teacher']['mandarin'] ? '是' : '否';
			$data['teacher']['cantonese'] = 1 == $data['teacher']['cantonese'] ? '是' : '否';
			$type = split(',', $data['teacher']['stuType']);
			$stu = array('小学生','初中生', '高中生','成人');
			$stuType = '';
			foreach ( $type as $v) {
				$stuType .= '  ' . $stu[$v];
			}
			$data['teacher']['stuType'] = substr($stuType, 1);
		}
		return $data;
	}

	function getInfo($uid) {
		$this->_tb->enableLink('work');
		return $this->_tb->find('user_id = ' . $uid);
	}

	function getPrintData($uid, $type) {
		if ( 'work' == $type )
			$this->_tb->enableLink('work');
		if ( 'teacher' == $type )
			$this->_tb->enableLink('teacher');

		$data = $this->_tb->find('user_id = ' . $uid);
		return $data;
	}

	function getBackData($username) {
		$data = $this->_tb->findByUsername($username, 'question, username');
		return $data;
	}

	function checkAnswer($data) {
		$cond = array('question' => $data['question'], 'answer' => md5($data['answer']));
		$data = $this->_tb->find($cond);
		return $data;
	}

	function changePassword($data) {
		$result = $this->_tb->updatePassword($_SESSION['getbackdata']['username'], $data['password']);
		unset($_SESSION['getbackdata']);
		return $result;
	}

	function checkUser($username) {
		return $this->_tb->existsUsername($username);
	}

	function getForm($type, $data = null) {
		$form = $this->_tb->form($type, $data);
		return $form;
	}
}
